# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
try:
    from .Weight import weight_male, weight_female
    from .Height import height_male, height_female
    from .BSA import calculate_bsa
except ImportError as e:
    print(f"错误：无法从 utils 子模块导入计算函数: {e}")
    raise

def process_population_data(df,
                           c0_w_m, c1_w_m, c0_w_f, c1_w_f,
                           c0_h_m, c1_h_m, c2_h_m, c0_h_f, c1_h_f, c2_h_f,
                           c1_param, weight_ex, height_ex,
                           cv_male_height, cv_male_weight,
                           cv_female_height, cv_female_weight,
                           seed=42):
    """
    处理人口 DataFrame，计算指标，应用波动，并按性别返回数据元组。
    返回: ((ages_m, ages_f), (heights_m, heights_f), ...)
    """
    df_male = df[df['gender'] == 'male'].copy()
    df_female = df[df['gender'] == 'female'].copy()

    ages_male = df_male['age'].to_numpy()
    ages_female = df_female['age'].to_numpy()
    ids_male = df_male['id'].to_numpy()
    ids_female = df_female['id'].to_numpy()

    # --- 计算基线身高和体重 ---
    heights_male = np.array([height_male(age, c0_h_m, c1_h_m, c2_h_m) for age in ages_male]) if len(ages_male) > 0 else np.array([])
    heights_female = np.array([height_female(age, c0_h_f, c1_h_f, c2_h_f) for age in ages_female]) if len(ages_female) > 0 else np.array([])
    weights_male = np.array([weight_male(age, c0_w_m, c1_w_m) for age in ages_male]) if len(ages_male) > 0 else np.array([])
    weights_female = np.array([weight_female(age, c0_w_f, c1_w_f) for age in ages_female]) if len(ages_female) > 0 else np.array([])

    # --- 应用 CV 波动 ---
    np.random.seed(seed)
    if len(heights_male) > 0 and cv_male_height > 0:
        heights_male *= np.random.normal(1, cv_male_height / 100, heights_male.shape)
        heights_male = np.maximum(heights_male, 0)
    if len(heights_female) > 0 and cv_female_height > 0:
        heights_female *= np.random.normal(1, cv_female_height / 100, heights_female.shape)
        heights_female = np.maximum(heights_female, 0)
    if len(weights_male) > 0 and cv_male_weight > 0:
        weights_male *= np.random.normal(1, cv_male_weight / 100, weights_male.shape)
        weights_male = np.maximum(weights_male, 0)
    if len(weights_female) > 0 and cv_female_weight > 0:
        weights_female *= np.random.normal(1, cv_female_weight / 100, weights_female.shape)
        weights_female = np.maximum(weights_female, 0)

    # --- 计算 BSA ---
    bsas_male = np.array([calculate_bsa(w, h, c1_param, weight_ex, height_ex)
                         for w, h in zip(weights_male, heights_male)]) if len(weights_male) > 0 else np.array([])
    bsas_female = np.array([calculate_bsa(w, h, c1_param, weight_ex, height_ex)
                           for w, h in zip(weights_female, heights_female)]) if len(weights_female) > 0 else np.array([])

    # --- 返回元组 ---
    return ((ages_male, ages_female),
            (heights_male, heights_female),
            (weights_male, weights_female),
            (bsas_male, bsas_female),
            (ids_male, ids_female))

# # --- 测试代码 (可选，保持不变) ---
# if __name__ == '__main__':
#     # ... (之前的测试代码) ...
